package com.ybkj.o2o.mc.service.impl;

import com.ybkj.o2o.mc.entity.PassengerGrade;
import com.ybkj.o2o.mc.entity.PassengerGradeAndArea;
import com.ybkj.o2o.mc.mapper.PassengerGradeAndAreaMapper;
import com.ybkj.o2o.mc.mapper.PassengerGradeMapper;
import com.ybkj.o2o.mc.service.PassengerGradeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by shine on 15/8/19.
 */
@Service
public class DefaultPassengerGradeService implements PassengerGradeService {

  @Autowired
  private PassengerGradeMapper passengerGradeMapper;

  @Autowired
  private PassengerGradeAndAreaMapper passengerGradeAndAreaMapper;

  @Override
  public int insert(PassengerGrade grade) {

    return passengerGradeMapper.insert(grade);
  }

  @Override
  public int update(PassengerGrade grade) {

    grade.setUpdated(new Date());

    return passengerGradeMapper.updateByPrimaryKey(grade);
  }

  @Override
  public PassengerGrade fetch(Long gradeId, Long companyId, String appKey) {

    PassengerGrade passengerGrade = passengerGradeMapper.selectByIdAndAppKey(gradeId, companyId, appKey);
    if (passengerGrade != null) {
      List<PassengerGradeAndArea> pas = passengerGradeAndAreaMapper.findByIdAndAppKey(passengerGrade.getId(), passengerGrade.getAppKey());
      passengerGrade.setPassengerGradeAndAreas(pas);
    }
    return passengerGrade;
  }

  @Override
  public int delete(Long gradeId, String appKey) {

    return passengerGradeMapper.deleteByIdAndAppkey(gradeId, appKey);
  }

  @Override
  public PassengerGrade findByNameAndAppKey(String name, String appKey) {

    return passengerGradeMapper.selectByNameAndAppKey(name, appKey);
  }

  @Override
  public Long countByNameAndAppKey(String name, Long companyId, String appKey) {

    return passengerGradeMapper.countByNameAndAppKey(name, companyId, appKey);
  }

  @Override
  public List<PassengerGrade> findByAppKey(String appKey) {

    return passengerGradeMapper.selectByAppKey(appKey);
  }

  @Override
  public PassengerGrade findDefault(Long companyId, String appKey) {
    return passengerGradeMapper.selectDefault(companyId, appKey);
  }


  @Override
  public List<PassengerGrade> findAll(int offset, int rows) {

    return passengerGradeMapper.selectByPage(offset, rows);
  }

  @Override
  public long count() {

    return passengerGradeMapper.countAll();
  }

  @Override
  public PassengerGrade findDefaultByCompanyId(Long companyId, String appKey) {

    return passengerGradeMapper.selectDefaultByCompanyId(companyId, appKey);
  }

  @Override
  public int updateDefaultGrade(boolean defaultGrade, Long id, Long companyId, String appKey) {

    return passengerGradeMapper.updateDefault(defaultGrade, id, companyId, appKey);
  }

  @Override
  public PassengerGrade findByNameAndCompanyId(String name, Long companyId, String appKey) {

    return passengerGradeMapper.findByNameAndCompanyId(name, companyId, appKey);
  }

  @Override
  public List<PassengerGrade> findByCompany(Long companyId, String appKey) {

    return passengerGradeMapper.findByCompany(companyId, appKey);
  }

  @Override
  public int updateCompanyName(Long companyId, String abbreviation, String appKey) {

    return passengerGradeMapper.updateCompanyName(companyId, abbreviation, appKey);
  }

  @Override
  public PassengerGrade findByIdAndAppKey(Long gradeId, String appKey) {

    return passengerGradeMapper.findByIdAndAppKey(gradeId,appKey);
  }
}
